from bs4 import BeautifulSoup
from .base import Handler
from event_engine.event_type import *
from event_engine.event import Event
from event_engine.engine import EventEngine
from models.task import *


class RentCircleHandler(Handler):
    """
    获取租房商圈
    """

    def process(self, event: Event, engine: EventEngine) -> None:
        html: str = event.data["html"]
        task: Task = event.data["task"]

        soup = BeautifulSoup(html, 'html.parser')
        uls = soup.find_all("ul", attrs={"data-target": "area"})
        if len(uls) == 2:
            a_list = uls[1].find_all("a")
        else:
            a_list = []
        links = [a["href"] for a in a_list]
        for link in links:
            url: str = task.get_host() + link
            engine.put(Event(EVENT_SAVE_TASK, {
                "type": TYPE_RENT_LIST,
                "url": url,
                "referer": task.url,
            }))

        task.complete()
